﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WarehouseManagement.Presenters.Import;
using WarehouseManagement.ViewModels.Import;

using System.Linq;
using WarehouseManagement.Views.Shared;
using WarehouseManagement.BusinessLogic.DTO;
using WarehouseManagement.Presenters.Export;
using WarehouseManagement.ViewModels.Export;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraSplashScreen;

namespace WarehouseManagement.Views.Export
{
    public partial class ViewExport : XtraForm, IView<ViewExportPresenter>, IViewExportViewModel
    {
        private ViewExportPresenter _presenter;

        public ViewExportPresenter Presenter
        {
            get { return _presenter; }
        }

        public ViewExport()
        {
            InitializeComponent();
            _presenter = SetupPresenter();
        }

        public ViewExportPresenter SetupPresenter()
        {
            var p = new ViewExportPresenter();
            p.RegisterViewModel(this);
            return p;
        }


        object IViewExportViewModel.ExportList
        {
            get
            {
                return grdExportList.DataSource as IEnumerable<ExportDetailDTO>;
            }
            set
            {



                grdExportList.DataSource = value;
            }
        }

        public decimal TotalImportValue
        {
            get
            {
                try
                {
                    return decimal.Parse(lblTotalImportValue.Tag.ToString());
                }
                catch
                {
                    return 0;
                }
            }

            set
            {
                lblTotalImportValue.Tag = value;
                lblTotalImportValue.Text = string.Format("Tổng thành tiền: {0:#,##0} VND", value);
            }
        }

        private void ViewExport_Load(object sender, EventArgs e)
        {

            txtFromDate.Properties.MaxValue = DateTime.Now.Date;
            cboOption_SelectedIndexChanged(sender, e);
            PrepareCellMergeSettings();
        }




        public DateTime FromDate
        {
            get
            {
                return txtFromDate.DateTime;
            }
            set
            {
                txtFromDate.DateTime = value;
            }
        }


        //private void txtSelectDate_EditValueChanged(object sender, EventArgs e)
        //{
        //    Presenter.LoadImportList();
        //}

        private void txtMerchandiseKW_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtToDate_EditValueChanged(object sender, EventArgs e)
        {
            if (FromDate > ToDate)
                FromDate = ToDate;

        }


        public DateTime ToDate
        {
            get
            {
                return txtToDate.DateTime;
            }
            set
            {
                txtToDate.DateTime = value;
            }
        }

        private void txtFromDate_EditValueChanged(object sender, EventArgs e)
        {
            if (FromDate > ToDate)
                ToDate = FromDate;
            //_presenter.LoadImportList();
        }

        private void btnRemoveMerchandise_Click(object sender, EventArgs e)
        {
            //SaleManagement.Views.Export.ExcludingWindow frm = new Export.ExcludingWindow();
            //int selectedRowHandle = grvImportList.GetSelectedRows().FirstOrDefault();

            //var selectedRow = grvImportList.GetRow(selectedRowHandle) as dynamic;

            //frm.ImportId = selectedRow.ImportId;
            //frm.MerchandiseId = selectedRow.MerchandiseId;

            //frm.Price = (decimal)selectedRow.UnitPrice;

            //frm.ShowDialog();
        }

        private ViewExportDetailDTO GetSelectedRow()
        {
            int selectedRowHandle = grvExportList.GetSelectedRows().FirstOrDefault();
            var selectedRow = grvExportList.GetRow(selectedRowHandle) as ViewExportDetailDTO;
            return selectedRow;
        }
        private void grvImportList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var item = GetSelectedRow();
            btnEdit.Enabled = (item != null);

        }

        private void btnView_Click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));          
            _presenter.LoadExportList();
            SplashScreenManager.CloseForm();
           
        }
       
        private void btnEdit_Click(object sender, EventArgs e)
        {
            var mainForm = this.ParentForm as MainForm;
            if (mainForm == null) return;
            if ((MainForm.FrmNewEditExport != null)
                || (MainForm.FrmNewEditExport != null && !MainForm.FrmNewEditExport.IsDisposed))
            {
                MainForm.FrmNewEditExport.Close();
            }
            var row = GetSelectedRow();
            if (row != null)
            {
                MainForm.FrmNewEditExport = new NewEditExport(NewEditExport.EditMode.EditExisting);
                MainForm.FrmNewEditExport.MdiParent = mainForm;

                MainForm.FrmNewEditExport.ExportId = row.ExportId;
                MainForm.FrmNewEditExport.ExportNumber = row.ExportNumber;
                MainForm.FrmNewEditExport.ExportDate = row.ExportDate;
                MainForm.FrmNewEditExport.MerchandiseId = row.MerchandiseId;

                //mainForm.frmImportNew.LoadImportListOnDemand();

                MainForm.FrmNewEditExport.Show();
            }
        }

        private void cboOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            AssignDate();
        }

        private void AssignDate()
        {
            var today = DateTime.Now;
            switch (cboOption.SelectedIndex)
            {
                case 1:
                    {
                        DateTime startOfWeek = DateTime.Today.AddDays(-1 * (int)(DateTime.Today.DayOfWeek));
                        this.FromDate = startOfWeek;
                        this.ToDate = startOfWeek.AddDays(6);
                        break;
                    }
                case 0:
                    {
                        this.FromDate = today;
                        this.ToDate = FromDate;
                        break;
                    }
                case 2:
                    {
                        this.FromDate = new DateTime(today.Year, today.Month, 1);
                        this.ToDate = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
                        break;
                    }
                case 3:
                    {
                        var period = GetPeriod(today.Month);
                        this.FromDate = GetStartDayOfPeriod(period, today.Year);
                        var todate = this.FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 4:
                    {
                        this.FromDate = new DateTime(today.Year, 1, 1);
                        this.ToDate = new DateTime(today.Year, 12, DateTime.DaysInMonth(today.Year, 12));
                        break;
                    }

                case 5:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 1, 1);
                        this.ToDate = new DateTime(today.Year, 1, DateTime.DaysInMonth(today.Year, 1));
                        break;
                    }
                case 6:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 2, 1);
                        this.ToDate = new DateTime(today.Year, 2, DateTime.DaysInMonth(today.Year, 2));
                        break;
                    }
                case 7:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 3, 1);
                        this.ToDate = new DateTime(today.Year, 3, DateTime.DaysInMonth(today.Year, 3));
                        break;
                    }
                case 8:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 4, 1);
                        this.ToDate = new DateTime(today.Year, 4, DateTime.DaysInMonth(today.Year, 4));
                        break;
                    }
                case 9:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 5, 1);
                        this.ToDate = new DateTime(today.Year, 5, DateTime.DaysInMonth(today.Year, 5));
                        break;
                    }
                case 10:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 6, 1);
                        this.ToDate = new DateTime(today.Year, 6, DateTime.DaysInMonth(today.Year, 6));
                        break;
                    }
                case 11:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 7, 1);
                        this.ToDate = new DateTime(today.Year, 7, DateTime.DaysInMonth(today.Year, 7));
                        break;
                    }
                case 12:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 8, 1);
                        this.ToDate = new DateTime(today.Year, 8, DateTime.DaysInMonth(today.Year, 8));
                        break;
                    }
                case 13:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 9, 1);
                        this.ToDate = new DateTime(today.Year, 9, DateTime.DaysInMonth(today.Year, 9));
                        break;
                    }
                case 14:
                    {
                        // Thang 1
                        this.FromDate = new DateTime(today.Year, 10, 1);
                        this.ToDate = new DateTime(today.Year, 10, DateTime.DaysInMonth(today.Year, 10));
                        break;
                    }
                case 15:
                    {
                        // Thang 11
                        this.FromDate = new DateTime(today.Year, 11, 1);
                        this.ToDate = new DateTime(today.Year, 11, DateTime.DaysInMonth(today.Year, 11));
                        break;
                    }
                case 16:
                    {
                        // Thang 12
                        this.FromDate = new DateTime(today.Year, 12, 1);
                        this.ToDate = new DateTime(today.Year, 12, DateTime.DaysInMonth(today.Year, 12));
                        break;
                    }

                case 17:
                    {
                        // Quy 1
                        this.FromDate = GetStartDayOfPeriod(1, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 18:
                    {
                        // Quy 2
                        this.FromDate = GetStartDayOfPeriod(2, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 19:
                    {
                        // Quy 3
                        this.FromDate = GetStartDayOfPeriod(3, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
                case 20:
                    {
                        // Quy 4
                        this.FromDate = GetStartDayOfPeriod(4, today.Year);
                        var todate = FromDate.AddMonths(2);
                        this.ToDate = new DateTime(todate.Year, todate.Month, DateTime.DaysInMonth(todate.Year, todate.Month));
                        break;
                    }
            }
        }

        private int GetPeriod(int month)
        {
            if (month >= 1 && month <= 3)
                return 1;
            else if (month >= 4 && month <= 6)
                return 2;
            else if (month >= 7 && month <= 9)
                return 3;
            else
                return 4;
        }

        private DateTime GetStartDayOfPeriod(int period, int year)
        {
            if (period < 1 || period > 4)
                throw new ArgumentException("Period invalid");
            return new DateTime(year, (period * 3) - 2, 1);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }





        public decimal TotalExportValue
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string MerchandiseSearchKeyword
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        private void btnExportNew_Click(object sender, EventArgs e)
        {
            var mainForm = this.ParentForm as MainForm;
            if (mainForm != null)
                mainForm.ShowExportNewForm();
        }

        private void grvExportList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = (GridView)sender;

            Point pt = view.GridControl.PointToClient(Control.MousePosition);

            DoRowDoubleClick(view, pt);
        }

        private void DoRowDoubleClick(GridView view, Point pt)
        {

            GridHitInfo info = view.CalcHitInfo(pt);

            if (info.InRow || info.InRowCell)
            {

                //string colCaption = info.Column == null ? "N/A" : info.Column.GetCaption();

                //MessageBox.Show(string.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle, colCaption));
                btnEdit_Click(this, null);
            }

        }

        private void DisableAllColumnCellMerge()
        {
            for (int i = 0; i < grvExportList.Columns.Count; i++)
            {
                grvExportList.Columns[i].OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            }
        }
        private void EnableCellMerge(string colFieldName, DevExpress.Utils.DefaultBoolean enabled)
        {

            grvExportList.Columns[colFieldName].OptionsColumn.AllowMerge = enabled;

        }

        private void PrepareCellMergeSettings()
        {
            grvExportList.OptionsView.AllowCellMerge = true;
            DisableAllColumnCellMerge();
            EnableCellMerge("ExportDate", DevExpress.Utils.DefaultBoolean.True);
            EnableCellMerge("ExportNumber", DevExpress.Utils.DefaultBoolean.True);
           
        }

        private void grvExportList_CellMerge(object sender, CellMergeEventArgs e)
        {
            if (e.Column.FieldName == "ExportDate")
            {

                DateTime value1 = (DateTime)grvExportList.GetRowCellValue(e.RowHandle1, e.Column);
                DateTime value2 = (DateTime)grvExportList.GetRowCellValue(e.RowHandle2, e.Column);
                e.Merge = (value1.Date == value2.Date);
                e.Handled = true;

            }
            else if (e.Column.FieldName == "ExportNumber")
            {
                string value1 = grvExportList.GetRowCellValue(e.RowHandle1, e.Column).ToString();
                string value2 = grvExportList.GetRowCellValue(e.RowHandle2, e.Column).ToString();
                e.Merge = (value1 == value2);
                e.Handled = true;
            }

        }

    }
}
